/*
 * @Author: szx
 * @Date: 2022-07-01 12:33:39
 * @LastEditTime: 2022-07-01 13:16:00
 * @Description:
 * @FilePath: \leetcode\200-299\241\241.js
 */
/**
 * @param {string} expression
 * @return {number[]}
 */
// 分治
const map = new Map(); // 表达式 -> 结果集
var diffWaysToCompute = function (expression) {
    if (map.has(expression)) return map.get(expression); // 直接返回
    const res = []; // 每个表达式对应的 结果集
    for (let i = 0; i < expression.length; i++) {
        const char = expression[i];
        if (char === '-' || char === '+' || char === '*') {
            // 分割 分而治之
            const lefts = diffWaysToCompute(expression.slice(0, i)); // slice包前不包后 返回0~i-1表达式的结果集
            const rights = diffWaysToCompute(expression.slice(i + 1)); // 返回i+1~最后表达式的结果集
            for (const left of lefts)
                for (const right of rights) {
                    switch (char) {
                        case '+':
                            res.push(left + right);
                            break;
                        case '-':
                            res.push(left - right);
                            break;
                        case '/':
                            res.push(left / right);
                            break;
                        default:
                            break;
                    }
                }
            // if (char === '+') for (const left of lefts) for (const right of rights) res.push(left + right);
            // else if (char === '-') for (const left of lefts) for (const right of rights) res.push(left - right);
            // else for (const left of lefts) for (const right of rights) res.push(left * right);
        }
    }
    if (!res.length) res.push(parseInt(expression)); // 直接存
    map.set(expression, res);
    return res;
};
